# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=go-swagger
pkgver=0.30.5
pkgrel=1
pkgdesc='Swagger 2.0 implementation in Go'
arch=('loong64' 'x86_64')
url='https://goswagger.io'
license=('Apache')
depends=('glibc')
makedepends=('git' 'go')
options=('!lto')
_commit='ab6a7885723430004f1d7db6395369b6e7f3370b'
source=("$pkgname::git+https://github.com/go-swagger/go-swagger#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS} \
    -X github.com/go-swagger/go-swagger/cmd/swagger/commands.Commit=$_commit \
    -X github.com/go-swagger/go-swagger/cmd/swagger/commands.Version=$pkgver" \
    -o build \
    ./cmd/...
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build/swagger

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md

  # shell completion
  cd cmd/swagger/completion
  install -vDm644 swagger.bash-completion "$pkgdir/usr/share/bash-completion/completions/swagger"
  install -vDm644 swagger.zsh-completion "$pkgdir/usr/share/zsh/site-functions/_swagger"
}
